package com.perstorp.cykelapp;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.view.View;
import com.google.android.gms.maps.model.LatLng;

class Coordinates {
	public String lat;
	public String lng;
}

public class GetMapCoordinates extends Activity {

	ArrayList<LatLng> markerPoints;
	View view;

	public GetMapCoordinates(View view) {
		this.view = view;

		setUpCoordinates();
	}

	public void setUpCoordinates() {
		XmlPullParserFactory pullParserFactory;
		try {
			pullParserFactory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = pullParserFactory.newPullParser();

			InputStream in_s = view.getContext().getApplicationContext()
					.getAssets().open("perstorp_map_coordinates.xml");
			parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
			parser.setInput(in_s, null);

			parseXML(parser);

		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void parseXML(XmlPullParser parser) throws XmlPullParserException,
			IOException {
		ArrayList<Coordinates> coordinates = null;
		int eventType = parser.getEventType();
		Coordinates currentCoordinates = null;
		String nameTest = "";

		while (eventType != XmlPullParser.END_DOCUMENT) {
			String name = "";
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				coordinates = new ArrayList<Coordinates>();
				break;
			case XmlPullParser.START_TAG:
				name = parser.getName();
				if (name.equalsIgnoreCase("latlng")) {
					currentCoordinates = new Coordinates();
				} else if (currentCoordinates != null) {
					if (name.equalsIgnoreCase("lat")) {
						currentCoordinates.lat = parser.nextText();
					} else if (name.equalsIgnoreCase("lng")) {
						currentCoordinates.lng = parser.nextText();
					}
				}
				break;
			case XmlPullParser.END_TAG:
				name = parser.getName();
				if (name.equalsIgnoreCase("latlng")
						&& currentCoordinates != null) {
					coordinates.add(currentCoordinates);
				}
			}
			eventType = parser.next();
		}

		setCoordinates(coordinates);
	}

	private void setCoordinates(ArrayList<Coordinates> coordinates) {

		Iterator<Coordinates> it = coordinates.iterator();
		markerPoints = new ArrayList<LatLng>();
		LatLng coord;
		while (it.hasNext()) {

			Coordinates currCoordinate = it.next();
			coord = new LatLng(Double.parseDouble(currCoordinate.lat),
					Double.parseDouble(currCoordinate.lng));
			markerPoints.add(coord);
		}
		
	}

	public ArrayList<LatLng> getCoordinates(int span) {
		ArrayList<LatLng> points = new ArrayList<LatLng>();
		points.add(markerPoints.get(span));
		points.add(markerPoints.get(span +1));
		return points;
	}
}
